.PHONY: all clean

include ${P_BASE}/global/utils/make/target
include ${P_BASE}/global/utils/make/local
include ${P_BASE}/global/utils/make/cmds
include ${P_BASE}/global/utils/make/escape

all: ${T_BASE}/mod_${MOD_NAME}_user

T_CC_USER_FLAGS ?= 	${T_CC_GLOBAL_FLAGS} -gstabs ${T_CC_FLAG_INC}${P_BASE} ${T_CC_FLAG_INC}${P_BASE}/modules/${MOD_NAME}/_user \
					${T_CC_FLAG_INC}${P_BASE}/modules/${MOD_NAME}/inc -nostdlib -nostdinc -fno-builtin

T_LIBGCC		?= $(shell $(call T_CC,,"",-print-libgcc-file-name))

SRCFILES:= $(shell $(call C_FIND,.,'(' '!' -regex '\./_.*' ')' -and '(' -iname '*.c' -or -iname '*.S' ')') | $(call C_SED,-e 's!\./!!g'))
OBJFILES:= $(addprefix ${T_BASE}/mod_${MOD_NAME}_user_,\
		     $(addsuffix .o,$(call E_ENCODE,${SRCFILES})))
DEPFILES:= $(OBJFILES:.o=.d)

-include $(DEPFILES)

${T_BASE}/mod_${MOD_NAME}_user_%.S.d: 
	@$(call C_ECHO,DEP $(call E_DECODE,$*).S)
	${V}$(call T_CC, $(call E_DECODE,$*).S,$@,-D__ASSEMBLY__ -MM ${T_CC_USER_FLAGS} -MT $(@:.d=.o))
	${V}$(call C_ECHO,"$(@:.d=.o): $(call E_DECODE,$*).S") >> $@

${T_BASE}/mod_${MOD_NAME}_user_%.c.d: 
	${V}$(call C_ECHO,DEP $(call E_DECODE,$*).c)
	${V}$(call T_CC, $(call E_DECODE,$*).c,$@,-MM ${T_CC_USER_FLAGS} -MT $(@:.d=.o))
	${V}$(call C_ECHO,"$(@:.d=.o): $(call E_DECODE,$*).c") >> $@

${T_BASE}/mod_${MOD_NAME}_user_%.S.o: ${T_BASE}/mod_${MOD_NAME}_user_%.S.d
	@$(call C_ECHO,CC $(call E_DECODE,$*).S)
	${V}$(call T_CC,$(call E_DECODE,$*).S,$@,-D__ASSEMBLY__ ${T_CC_USER_FLAGS} ${T_CC_FLAG_OBJ})

${T_BASE}/mod_${MOD_NAME}_user_%.c.o: ${T_BASE}/mod_${MOD_NAME}_user_%.c.d
	@$(call C_ECHO,CC $(call E_DECODE,$*).c)
	${V}$(call T_CC,$(call E_DECODE,$*).c,$@,${T_CC_USER_FLAGS} ${T_CC_FLAG_OBJ})

${T_BASE}/mod_${MOD_NAME}_user: ${OBJFILES} ${T_BASE}/libcommon_*.o 
	@$(call C_ECHO,LD $@)
	${V}$(call T_LD,$^ ${T_LIBGCC} \
#		${T_BASE}/mod_${MOD_NAME}_user-CAIRO*.o \
		,$@,-N -T user.ld)
